<?php
/***************************************************************************
 *                              edit_pirep.php
 *                            -------------------
 *                       (APU) Automated Pilot Utility
 *   begin                : Saturday, Dec 18, 2004
 *   copyright            : (C) 2004 SF Games
 *   email                : techsupport@sf-games.com
 ***************************************************************************/

	$apus_root_path = '../include/';
	include($apus_root_path . 'common.php');
	include($apus_root_path . 'config.php');

	// Get the pirep being requested
	parse_str($_SERVER['QUERY_STRING']);
	$pirepId = $pirep;

	// HTML header
	echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">";
	echo "<html>";
	echo "<head>";
	echo "<title>APU Administration</title>";
	echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">";
	echo "</head>";
	echo "<body>";

	// Attempt connect to database
	$db = mysql_connect($dbhost, $dbuname, $dbpass)
		or die("Could not connect to the SQL server<br>");
	mysql_select_db($dbname);

	// Get the PIREP from the database
	$query  = 'SELECT * FROM " .$table_prefix ."PIREP WHERE id = ' .sqlfix($pirepId);
	$result = mysql_query($query);
	if ( !$result )
		die("Query for PIREP failed.<br>Click your browsers back button to try again.<br>If you continue to receive this error, contact the webmaster<br>" .mysql_error());
	$pirep = mysql_fetch_array($result, MYSQL_ASSOC);

	// Get some pilot information
	$query  = 'SELECT * " .$table_prefix ."FROM PILOTS WHERE PILOT_ID = ' .sqlfix($pirep["PILOT_ID"]);
	$result = mysql_query($query);
	if ( !$result )
		die("Query for PIREP failed.<br>Click your browsers back button to try again.<br>If you continue to receive this error, contact the webmaster<br>");
	$pilot = mysql_fetch_array($result, MYSQL_ASSOC);

	//
	// Display PIREP Edit form with values from database record
	//

	echo '<div align="center">';
	  echo '<p><font face="Verdana, Arial, Helvetica, sans-serif" size="5" color="#840000"><b><font size="4">APU Administration - Edit PIREP</font></b></font></p>';
	  echo '<table width="395" border="0" cellspacing="1" cellpadding="1">';
		echo '<tr bgcolor="#FFFFCC">';
		  echo '<td>';
		  	echo '<div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="3" color="#840000"><b>' .$pilot["FIRST_NAME"] .' ' .$pilot["LAST_NAME"] .' - ' .$pilot["PILOT_ID"] .'</b></font></div>';
		  echo '</td>';
		echo '</tr>';
	  echo '</table>';
	echo '</div>';

	echo '<table width="395" border="0" cellpadding="0" cellspacing="0" align="center">';
	  echo '<tr> ';
		echo '<td width="500" height="200" bgcolor="#FFFFCC">';
		  echo '<form name="pirep_form" method="post" action="pirep_update.php">';
			echo '<table width="100%" border="1" cellspacing="1" cellpadding="1">';
			  echo '<tr> ';
				echo '<td>';
				  echo '<div align="left"><font face="Verdana, Arial, Helvetica, sans-serif" color="#FF0000"><b><font size="2">* Delete PIREP:</font></b></font>';
					echo '<input type="checkbox" name="delete_pirep" value="1">';
					echo '<font size="1" face="Verdana, Arial, Helvetica, sans-serif" color="#FF0000">- This will permanently delete PIREP.</font></div>';
				echo '</td>';
			  echo '</tr>';
			echo '</table>';
			echo '<br>';
			echo '<table width="100%" border="0" cellspacing="2" cellpadding="1">';
			  echo '<tr>';
				echo '<td>';
				  echo '<div align="right"><font face="Verdana, Arial, Helvetica, sans-serif" color="#840000"><b><font size="2">Flight Date :</font></b></font></div>';
				echo '</td>';
				echo '<td>';
				  echo '<input type="text" name="flight_date" size="14" maxlength="10" value="' .$pirep["FLIGHT_DATE"] .'">';
				echo '</td>';
			  echo '</tr>';
			  echo '<tr>';
				echo '<td> ';
				  echo '<div align="right"><b><font color="#840000" face="Verdana, Arial, Helvetica, sans-serif" size="2">Aircraft Type :</font></b></div>';
				echo '</td>';
				echo '<td>';
				  echo '<select name="ac_type" size="1">';
				  // The A/C include list needs special attention here, to preselect the PIREPS choice of A/C
				  // versus the simple php include('../aircraft.inc'); statement found in the PIREP submit form
				  //include('../aircraft.inc');
				  if ( ($handle = fopen("../aircraft.inc", "r")) == FALSE )
				  	include('../aircraft.inc');
				  else
				  {
				  	while ( !feof($handle) )
					{
				  		// Read each entry
						$sHTML = fgets($handle);

						// stripos available in PHP 5, not 4.x, so make our compares lower case
						$sType = strtolower($pirep["AC_TYPE"]);

						// See if this line of HTML text contains the PIREP aircraft
						$pos = strpos($sHTML, $sType);
						// Spit out HTML if not our A/C
						if ( $pos === false )
							echo $sHTML;
						else
						{
							// Find position of last quote so we can add the HTML attribute selected, to have this A/C selected for us
							$pos = strrpos($sHTML, "\"");
							$sHTML = substr_replace($sHTML, '", selected', $pos+1, 0);
							echo $sHTML;
						}
					}

					// All done
					fclose($handle);
				  }
				  echo '</select>';
				echo '</td>';
			  echo '</tr>';
			  echo '<tr>';
				echo '<td>';
				  echo '<div align="right"><b><font color="#840000" face="Verdana, Arial, Helvetica, sans-serif" size="2">Tail # :</font></b></div>';
				echo '</td>';
				echo '<td>';
				  echo '<input type="text" name="tail_num" size="20" maxlength="20" value="' .$pirep["TAIL_NUM"] .'">';
				echo '</td>';
			  echo '</tr>';
			  echo '<tr>';
				echo '<td>';
				  echo '<div align="right"><b><font color="#840000" face="Verdana, Arial, Helvetica, sans-serif" size="2">Flight # :</font></b></div>';
				echo '</td>';
				echo '<td>';
				  echo '<input type="text" name="flight_num" size="20" maxlength="20" value="' .$pirep["FLIGHT_NUM"] .'">';
				echo '</td>';
			  echo '</tr>';
			  echo '<tr>';
				echo '<td>';
				  echo '<div align="right"><b><font color="#840000" face="Verdana, Arial, Helvetica, sans-serif" size="2">Departure Airport :</font></b></div>';
				echo '</td>';
				echo '<td>';
				  echo '<input type="text" name="departure" size="20" maxlength="20" value="' .$pirep["DEPART"] .'">';
				echo '</td>';
			  echo '</tr>';
			  echo '<tr>';
				echo '<td>';
				  echo '<div align="right"><b><font color="#840000" face="Verdana, Arial, Helvetica, sans-serif" size="2">Destination Airport :</font></b></div>';
				echo '</td>';
				echo '<td>';
				  echo '<input type="text" name="destination" size="20" maxlength="20" value="' .$pirep["DEST"] .'">';
				echo '</td>';
			  echo '</tr>';
			  echo '<tr>';
				echo '<td>';
				  echo '<div align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="#840000"><b>Time Out GMT :</b></font></div>';
				echo '</td>';
				echo '<td>';
				  echo '<input type="text" name="time_out" size="20" maxlength="20" value="' .$pirep["TIME_OUT"] .'">';
				echo '</td>';
			  echo '</tr>';
			  echo '<tr>';
				echo '<td>';
				  echo '<div align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="#840000"><b>Time In GMT :</b></font></div>';
				echo '</td>';
				echo '<td>';
				  echo '<input type="text" name="time_in" size="20" maxlength="20" value="' .$pirep["TIME_IN"] .'">';
				echo '</td>';
			  echo '</tr>';
			  echo '<tr>';
				echo '<td>';
				  echo '<div align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="#840000"><b>Fixed Wing Time :</b></font></div>';
				echo '</td>';
				echo '<td>';
				  echo '<input type="text" name="time_fixed" size="20" maxlength="20" value="' .$pirep["TIME_FIXED"] .'">';
				echo '</td>';
			  echo '</tr>';
  			  echo '<tr>';
				echo '<td>';
				  echo '<div align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="#840000"><b>Helo Time :</b></font></div>';
				echo '</td>';
				echo '<td>';
				  echo '<input type="text" name="time_helo" size="20" maxlength="20" value="' .$pirep["TIME_HELO"] .'">';
				echo '</td>';
			  echo '</tr>';
			  echo '<tr>';
				echo '<td>';
				  echo '<div align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="#840000"><b>Fuel Out :</b></font></div>';
				echo '</td>';
				echo '<td>';
				  echo '<input type="text" name="fuel_out" size="20" maxlength="20" value="' .$pirep["FUEL_OUT"] .'">';
				echo '</td>';
			  echo '</tr>';
			  echo '<tr>';
				echo '<td>';
				  echo '<div align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="#840000"><b>Fuel In :</b></font></div>';
				echo '</td>';
				echo '<td>';
				  echo '<input type="text" name="fuel_in" size="20" maxlength="20" value="' .$pirep["FUEL_IN"] .'">';
				echo '</td>';
			  echo '</tr>';
			  echo '<tr>';
				echo '<td>';
				  echo '<div align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="#840000"><b>Fuel Burn :</b></font></div>';
				echo '</td>';
				echo '<td>';
				  echo '<input type="text" name="fuel_burn" size="20" maxlength="20" value="' .$pirep["FUEL_BURN"] .'">';
				echo '</td>';
			  echo '</tr>';
			  echo '<tr>';
				echo '<td>';
				  echo '<div align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="#840000"><b>PAX :</b></font></div>';
				echo '</td>';
				echo '<td>';
				  echo '<input type="text" name="pax" size="20" maxlength="20" value="' .$pirep["PAX"] .'">';
				echo '</td>';
			  echo '</tr>';
			  echo '<tr>';
				echo '<td>';
				  echo '<div align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="#840000"><b>Cargo :</b></font></div>';
				echo '</td>';
				echo '<td>';
				  echo '<input type="text" name="cargo" size="20" maxlength="20" value="' .$pirep["CARGO"] .'">';
				echo '</td>';
			  echo '</tr>';
			  echo '<tr>';
				echo '<td>';
				  echo '<div align="right"><b><font color="#840000" face="Verdana, Arial, Helvetica, sans-serif" size="2">Comments:</font></b></div>';
				echo '</td>';
				echo '<td>';
				  echo '<textarea name="comments" cols="20" rows="4">' .$pirep["COMMENTS"] .'</textarea>';
				echo '</td>';
			  echo '</tr>';
			echo '</table>';

		    echo "<p align=\"center\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" color=\"#0033FF\"><b><font size=\"2\">Notes</font></b></font></p>\n";
			echo "<table width=\"100%\" border=\"0\" cellspacing=\"3\" cellpadding=\"1\">\n";
			  echo "<tr>\n";
				echo "<td width=\"21%\"><div align=\"right\"><font face=\"Verdana, Arial, Helvetica, sans-serif\"><b><font color=\"#FF0000\" size=\"1\">*</font></b></font></div></td>\n";
				echo "<td width=\"79%\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\">This will permanently deleted the PIREP from the database.</font></td>\n";
			  echo "</tr>\n";
			  echo "<tr>\n";
				echo "<td width=\"21%\"><div align=\"right\"><font face=\"Verdana, Arial, Helvetica, sans-serif\"><b><font color=\"#FF0000\" size=\"1\">-</font></b></font></div></td>\n";
				echo "<td width=\"79%\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\">Dates must be in mySQL format YYYY-MM-DD.</font></td>\n";
			  echo "</tr>\n";
			  echo "<tr>\n";
				echo "<td width=\"21%\"><div align=\"right\"><font face=\"Verdana, Arial, Helvetica, sans-serif\"><b><font color=\"#840000\" size=\"1\">-</font></b></font></div></td>\n";
				echo "<td width=\"79%\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\">Some characters, such as # are not accepted by mySQL.</font></td>\n";
			  echo "</tr>\n";
			  echo "<tr>\n";
				echo "<td width=\"21%\"><div align=\"right\"><font face=\"Verdana, Arial, Helvetica, sans-serif\"><b><font color=\"#840000\" size=\"1\">-</font></b></font></div></td>\n";
				echo "<td width=\"79%\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\">This form does no validation for correctness, so be careful.</font></td>\n";
			  echo "</tr>\n";
			echo "</table>\n";

			echo '<p align="center">';
			  echo '<input type="hidden" name="pirepid" value="' .$pirep["id"] .'">';
			  echo '<input type="submit" name="Submit" value="Submit">';
			echo '</p>';
		  echo '</form>';
		echo '</td>';
	  echo '</tr>';
	echo '</table>';

	// HTML footer
	echo '<div align="center">';
	echo("<p><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\" color=\"#999999\">" .$APU_COPYRIGHT ."</font></p>");
	echo '</div>';
	echo "</body>";
	echo "</html>";
?>
